﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace BanmaytinhDAL
{
    public class LoaihangDAL : SQLDataProvider
    {
        private static LoaihangInfo obj;
        public  LoaihangInfo Loaihang_GetById(string Maloai)
        {
            BanmaytinhDAL.LoaihangInfo obj = new BanmaytinhDAL.LoaihangInfo();

            using (SqlCommand dbCmd = new SqlCommand("sp_Loaihang_GetById", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Maloai));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        obj = obj.DonvitinhIDataReader(dr);
                    }
                }
                dr.Close();

            }
            return obj;
        }
        public List<LoaihangInfo> Loaihang_GetByAll()
        {
            List<BanmaytinhDAL.LoaihangInfo> list = new List<BanmaytinhDAL.LoaihangInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Loaihang_GetByAll", GetConnection()))
            {
                BanmaytinhDAL.LoaihangInfo obj = new BanmaytinhDAL.LoaihangInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;

                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.DonvitinhIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }


        public bool Insert(LoaihangInfo obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_Loaihang_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Tenloai", (obj.Tenloai == null) ? String.Empty : obj.Tenloai);

                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(LoaihangInfo obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_Loaihang_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Id", (obj.Maloai == null) ? String.Empty : obj.Maloai);
                    cmd.Parameters.AddWithValue("@Tenloai", (obj.Tenloai == null) ? String.Empty : obj.Tenloai);

                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string Maloai)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_Loaihang_Delete";
                    cmd.Parameters.AddWithValue("@Id", Maloai);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }




    }
}
